<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // window.Promise();
    // 是一个构造函数， 是跟随在window方法上的

    //创建promise实例
    let promise = new Promise((resolve, reject) => {
        if (true) {
            resolve("1")
        } else {
            reject("2")
        }
    })

    // 三种状态
    //padding 
    //fullfilled
    //rejected

    promise.then((data) => {
        console.log(data)
    })
    promise.catch((err) => {
        console.log(err)
    })
    promise.finally(() => { //这里不传参
        console.log("不管如何都会执行这个回调函数")
    })

    // 可以连写
    // promise.then("回调函数").finally("回调函数");

    // 先将对象转化为promise对象
    obj = {
        name: "guozihao",
        age: 18,
        jump: () => {
            console.log("111111")
        }
    }

    p1 = Promise.resolve(obj);
    p1.then((re) => {
        console.log(res)
    })

    p2 = Promise.reject(obj);
    p2.catch((re) => {
        console.log(res)
    })

    Promise.all([p1, p2])


    // async await

    var p1 = new Promise((resolve, reject) => {
        $.ajax({
            method: "get",
            url: "url",
            success: (data) => {
                resolve(data)
            },
            error: (err) => {
                reject(err)
            }
        })
    })

    $(".btn").click(async () => {
        let result = await p1;
        console.log(result)
    })
</script>
<script>

</script>

</html>